package sistema;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.media.Buffer;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;

@SuppressWarnings("unused")
public class AudioTest{
	private JButton play, pause, stop,open;
	private AudioPlayer audio = null;
	private JFrame frame;
	private JFileChooser fileChooser;
	private JLabel status;

	public static void main(String[] args) {
		AudioTest t1 = new AudioTest();
		t1.inicia();
	}

	private void inicia(){
		fileChooser= new JFileChooser();
		fileChooser.setFileFilter(new FileFilter() {
			
			@Override
			public String getDescription() {
				return null;
			}
			
			@Override
			public boolean accept(File f) {
				if (f.isDirectory()) {
			        return true;
			    }

			    String extension = getExtension(f);
			    if (extension != null) {
			        if (extension.equals("mp3")){
			                return true;
			        } else {
			            return false;
			        }
			    }

			    return false;
			}
		});
		frame = new JFrame("Player de Audio mp3");
		frame.setLayout(new FlowLayout(FlowLayout.CENTER));
		frame.setVisible(true);
		frame.setSize(400,100);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		play = new JButton("Play"); 
		stop = new JButton("Stop"); 
		pause = new JButton("Pause");
		open = new JButton("Open");

		status = new JLabel("Clique em Open para selecionar um arquivo de som");
		
		frame.add(play);
		frame.add(pause);
		frame.add(stop);
		frame.add(open);
		frame.add(status);
		
		addListnerOpen();
		
	}
	
	private void addListnerOpen(){
			open.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					if(e.getSource() == open){
						if(fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION){
							try {
								audio = new AudioPlayer(fileChooser.getSelectedFile());
								play.addActionListener(new ActionListener() {
									
									@Override
									public void actionPerformed(ActionEvent arg0) {
										if(arg0.getSource() == play){
											audio.play();
										}
									}
								});
								stop.addActionListener(new ActionListener() {
									
									@Override
									public void actionPerformed(ActionEvent e) {
										if(e.getSource() == stop){
											audio.stop();
											addListnerOpen();
											status.setText("Clique em Open para selecionar um arquivo de som");
											repaint(frame);
										}
									}
								});
								pause.addActionListener(new ActionListener() {
									
									@Override
									public void actionPerformed(ActionEvent e) {
										if(e.getSource() == pause){
											audio.pause();
											status.setText("Play para continuar...");
											repaint(frame);
										}
									}
								});
							} catch (Exception e1) {
								System.out.println(e1.getMessage());
							}
							audio.play();
							open.removeActionListener(this);
							status.setText("Tocando: "+fileChooser.getSelectedFile().getName());
							repaint(frame);
						}						
					}					
				}				
			});
		}
	
	private void repaint(final Component componente){
		java.awt.EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				componente.repaint();
				componente.revalidate();
			}
		});
	}

	
	private String getExtension(File f) {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');

        if (i > 0 &&  i < s.length() - 1) {
            ext = s.substring(i+1).toLowerCase();
        }
        return ext;
    }
}